package cn.springcloudtest.service;

import cn.springcloudtest.mapper.TopicMapper;
import cn.springcloudtest.pojo.Topic;
import cn.springcloudtest.utils.Constants;
import cn.springcloudtest.utils.EmptyUtils;
import cn.springcloudtest.utils.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName:springcloud-topic-provider
 * @Author:赵伟杰
 * @Date:2021年09月05日 12:01
 */
@RestController
public class RestTopicService {

    @Resource
    private TopicMapper topicMapper;

    @RequestMapping("/local/getTopicById")
    public Topic getTopicById(@RequestParam Integer id) throws Exception {
        return topicMapper.getTopicById(id);
    }

    @RequestMapping("/local/getTopicListByMap")
    public List<Topic> getTopicListByMap(@RequestBody Map<String, Object> param) throws Exception {
        System.out.println("我是Topic1");
        return topicMapper.getTopicListByMap(param);
    }

    @RequestMapping("/local/getTopicCountByMap")
    public Integer getTopicCountByMap(@RequestBody Map<String, Object> param) throws Exception {
        return topicMapper.getTopicCountByMap(param);
    }

    @RequestMapping("/local/AddTopic")
    public Integer addTopic(@RequestBody Topic topic) throws Exception {
        return topicMapper.insertTopic(topic);
    }

    @RequestMapping("/local/ModifyTopic")
    public Integer modifyTopic(@RequestBody Topic topic) throws Exception {
        return topicMapper.updateTopic(topic);
    }

    @RequestMapping("/local/deleteTopicById")
    public Integer deleteTopicById(@RequestParam Integer id) throws Exception {
        return topicMapper.deleteTopicById(id);
    }

    @RequestMapping("/local/queryTopicPageByMap")
    public Page<Topic> queryTopicPageByMap(@RequestBody Map<String, Object> param) throws Exception {
        Integer pageNo = (Integer) param.get("pageNo");
        Integer pageSize = (Integer) param.get("pageSize");
        Integer total = topicMapper.getTopicCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.PageNumber.pageNo : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.PageNumber.pageSize : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        page.setRows(topicMapper.getTopicListByMap(param));
        return page;
    }


}
